package inc.ryusys;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class Calculator2 extends Activity implements OnClickListener,
		OnItemSelectedListener {
	/** Called when the activity is first created. */

	private static final String ZERO = "0";
	private static final String DEF_TYPE = "id";
	private static final String DEF_PACK = "inc.ryusys";
	private static final String[] IdName = { "num_0", "num_1", "num_2",
			"num_3", "num_4", "num_5", "num_6", "num_7", "num_8", "num_9",
			"plus", "times", "minus", "divide", "equal", "clear", "num_a",
			"num_b", "num_c", "num_d", "num_e", "num_f" };
	public int signs = 0;
	public int be_signs = 0;
	public int conv = 0;
	public int input = 0;
	public int caluc = 0;
	public Long seg;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

//		View Button_0 = this.findViewById(R.id.num_0);
//		Button_0.setOnClickListener(this);
//		View Button_1 = this.findViewById(R.id.num_1);
//		Button_1.setOnClickListener(this);
//		View Button_2 = this.findViewById(R.id.num_2);
//		Button_2.setOnClickListener(this);
//		View Button_3 = this.findViewById(R.id.num_3);
//		Button_3.setOnClickListener(this);
//		View Button_4 = this.findViewById(R.id.num_4);
//		Button_4.setOnClickListener(this);
//		View Button_5 = this.findViewById(R.id.num_5);
//		Button_5.setOnClickListener(this);
//		View Button_6 = this.findViewById(R.id.num_6);
//		Button_6.setOnClickListener(this);
//		View Button_7 = this.findViewById(R.id.num_7);
//		Button_7.setOnClickListener(this);
//		View Button_8 = this.findViewById(R.id.num_8);
//		Button_8.setOnClickListener(this);
//		View Button_9 = this.findViewById(R.id.num_9);
//		Button_9.setOnClickListener(this);
//		View Button_plus = this.findViewById(R.id.plus);
//		Button_plus.setOnClickListener(this);
//		View Button_minus = this.findViewById(R.id.minus);
//		Button_minus.setOnClickListener(this);
//		View Button_times = this.findViewById(R.id.times);
//		Button_times.setOnClickListener(this);
//		View Button_divide = this.findViewById(R.id.divide);
//		Button_divide.setOnClickListener(this);
//		View Button_equal = this.findViewById(R.id.equal);
//		Button_equal.setOnClickListener(this);
//		View Button_clear = this.findViewById(R.id.clear);
//		Button_clear.setOnClickListener(this);
//		View Button_a = this.findViewById(R.id.num_a);
//		Button_a.setOnClickListener(this);
//		View Button_b = this.findViewById(R.id.num_b);
//		Button_b.setOnClickListener(this);
//		View Button_c = this.findViewById(R.id.num_c);
//		Button_c.setOnClickListener(this);
//		View Button_d = this.findViewById(R.id.num_d);
//		Button_d.setOnClickListener(this);
//		View Button_e = this.findViewById(R.id.num_e);
//		Button_e.setOnClickListener(this);
//		View Button_f = this.findViewById(R.id.num_f);
//		Button_f.setOnClickListener(this);

		 View[] Button_array;
		 Button_array = new View[IdName.length];
		 for (int i = 0; i < IdName.length; i++) {
		 int tmp_id = getResources().getIdentifier(IdName[i], DEF_TYPE,
		 DEF_PACK);
		 Button_array[i] = this.findViewById(tmp_id);
		 Button_array[i].setOnClickListener(this);
		 }
		Spinner segment = (Spinner) findViewById(R.id.segment);
		segment.setOnItemSelectedListener(this);

	}

	public void onClick(View v) {

		EditText answer = (EditText) findViewById(R.id.answer);
		Button ClickB = (Button) v;
		Spinner segment = (Spinner) findViewById(R.id.segment);

		if (segment.getSelectedItemId() == 0) {
			conv = Integer.valueOf(answer.getText().toString());
		} else {
			conv = Integer.parseInt(answer.getText().toString(), 16);
		}

		switch (v.getId()) {
		case R.id.clear:
			if (be_signs == R.id.equal) {
				signs = 0;
			}
			answer.setText(ZERO);
			break;
		case R.id.plus:
		case R.id.minus:
		case R.id.times:
		case R.id.divide:
		case R.id.equal:
			switch (signs) {
			case R.id.plus:
				// 足し算
				caluc = input + conv;
				break;
			case R.id.minus:
				// 引き算
				caluc = input - conv;
				break;
			case R.id.times:
				// 掛け算
				caluc = input * conv;
				break;
			case R.id.divide:
				// 割り算
				caluc = input / conv;
				break;
			default:
				caluc = conv;
				break;
			}

			if (segment.getSelectedItemId() == 0) {
				answer.setText(String.valueOf(caluc));
			} else {
				answer.setText(Integer.toHexString(caluc).toUpperCase());
			}

			signs = v.getId();
			input = caluc;
			break;
		default:
			if (signs != 0) {
				if (be_signs == R.id.equal) {
					signs = 0;
					answer.setText(ClickB.getText());
				} else if (be_signs == R.id.plus || be_signs == R.id.minus
						|| be_signs == R.id.times || be_signs == R.id.divide
						|| conv == 0) {
					answer.setText(ClickB.getText());
				} else {
					answer.setText(answer.getText().toString()
							+ ClickB.getText());
				}
			} else if (conv != 0) {
				answer.setText(answer.getText().toString() + ClickB.getText());
			} else {
				answer.setText(ClickB.getText());
			}
			break;
		}

		be_signs = v.getId();

		if (segment.getSelectedItemId() == 0) {
			conv = Integer.valueOf(answer.getText().toString());
		} else {
			conv = Integer.parseInt(answer.getText().toString(), 16);
		}

	}

	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO 自動生成されたメソッド・スタブ
		EditText answer = (EditText) findViewById(R.id.answer);
		Button Button_a = (Button) findViewById(R.id.num_a);
		Button Button_b = (Button) findViewById(R.id.num_b);
		Button Button_c = (Button) findViewById(R.id.num_c);
		Button Button_d = (Button) findViewById(R.id.num_d);
		Button Button_e = (Button) findViewById(R.id.num_e);
		Button Button_f = (Button) findViewById(R.id.num_f);

		if (arg0.getSelectedItemId() == 0) {
			Button_a.setEnabled(false);
			Button_b.setEnabled(false);
			Button_c.setEnabled(false);
			Button_d.setEnabled(false);
			Button_e.setEnabled(false);
			Button_f.setEnabled(false);
			answer.setText(String.valueOf(conv));
		} else {
			Button_a.setEnabled(true);
			Button_b.setEnabled(true);
			Button_c.setEnabled(true);
			Button_d.setEnabled(true);
			Button_e.setEnabled(true);
			Button_f.setEnabled(true);
			answer.setText(Integer.toHexString(Integer.valueOf(conv))
					.toUpperCase());
		}

	}

	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO 自動生成されたメソッド・スタブ

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		Spinner segment = (Spinner) findViewById(R.id.segment);
		outState.putLong("seg", segment.getSelectedItemId());
		outState.putInt("conv", conv);
	}

	protected void onrestoreInstanceState(Bundle saveInstanceState) {
		super.onRestoreInstanceState(saveInstanceState);
		seg = saveInstanceState.getLong("seg");
		conv = saveInstanceState.getInt("conv");
		Spinner segment = (Spinner) findViewById(R.id.segment);
		segment.setSelection(Integer.valueOf(seg.toString()));
	}

}